<?php
namespace Goyello\MVC\Model;

class Address extends Model
{
    CONST TABLE = 'ADDRESS';

    public function __construct()
    {
        parent::__construct();
    }

    public function add($address)
    {
        $sql = "
            INSERT INTO ". self::TABLE . "
                (LABEL,STREET,HOUSENUMBER,POSTALCODE,CITY,COUNTRY)
            VALUES
                ('". $this->escape($address['LABEL']) ."',
                 '". $this->escape($address['STREET'])  ."',
                 '". $this->escape($address['HOUSENUMBER']) ."',
                 '". $this->escape($address['POSTALCODE']) ."',
                 '". $this->escape($address['CITY']) ."',
                 '". $this->escape($address['COUNTRY']) ."')";

        return $this->exec($sql);
    }

    public function delete($id)
    {
        $sql = "
            DELETE FROM ". self::TABLE . "
                WHERE ADDRESSID = " . intval($id);
        return $this->exec($sql);
    }

    public function deleteAll()
    {
        $sql = "DELETE FROM ". self::TABLE;
        return $this->exec($sql);
    }

    public function get($id)
    {
        $sql = "
            SELECT * FROM ". self::TABLE . "
                WHERE
                    ADDRESSID = " . intval($id);

        return $this->fetchAllAssoc($sql);
    }

    public function getAll()
    {
         $sql = "SELECT * FROM ". self::TABLE;
        return $this->fetchAllAssoc($sql);
    }

    public function update($id, $address)
    {
         $sql = "
            UPDATE ". self::TABLE . "
                SET
                    LABEL='". $this->escape($address['LABEL']) ."',
                    STREET='". $this->escape($address['STREET']) ."',
                    HOUSENUMBER='". $this->escape($address['HOUSENUMBER']) ."',
                    POSTALCODE='". $this->escape($address['POSTALCODE']) ."',
                    CITY='". $this->escape($address['CITY']) ."',
                    COUNTRY='". $this->escape($address['COUNTRY']) ."'
                WHERE
                    ADDRESSID = " . intval($id);

        return $this->exec($sql);
    }
}
